﻿namespace Example
{
    using System.Linq;
    using Example.Models;
    using Xunit;

    public sealed class Linq2ObjectsFacts
    {
        [Fact]
        public void select_from()
        {
            var list = new[]
            {
                10, 100, 1000
            };
            var query = from number in list
                        where number < 1000
                        select number;

            const int expected = 110;
            var actual = query.Sum();

            Assert.Equal(expected, actual);
        }

        [Fact]
        public void select_from_typeofString()
        {
            var query = from method in typeof(string).GetMethods()
                        where method.IsStatic && method.IsPublic
                        orderby method.Name
                        group method by method.Name
                        into g
                        orderby g.Count()
                        select new
                        {
                            Name = g.Key,
                            Overloads = g.Count()
                        };

            Assert.Equal(13, query.Count());
        }

        [Fact]
        public void select_into_anonymouse_type()
        {
            var list = new[]
            {
                new CartesianCoordinates(10, 20),
                new CartesianCoordinates(100, 200),
                new CartesianCoordinates(1000, 2000)
            };

            var query = from coord in list
                        where coord.X > 10 && coord.X < 1000
                        select new
                        {
                            coord.Y
                        };

            Assert.Equal(1, query.Count());
            Assert.Equal(200, query.First().Y);
        }
    }
}